package com.paic.mhis.hcpms.quota.settlement.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.quota.settlement.biz.service.FeeExecuteInfoService;
import com.paic.mhis.hcpms.quota.settlement.biz.service.KpiRegionCodeService;
import com.paic.mhis.hcpms.quota.settlement.web.vo.FeeExeInfoParamsVO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.pingan.um.client.util.StringUtil;

@Controller
@RequestMapping("/feeExecute/")
public class FeeExecuteController {
	@BizServicesProxy(beanName = "feeExecuteInfoService")
	public FeeExecuteInfoService feeExecuteInfoService;
	@BizServicesProxy(beanName = "kpiRegionCodeService")
	public KpiRegionCodeService kpiRegionCodeService;

	/**
	 * 费用执行情况charts data
	 */
	@RequestMapping("getFeeExecuteInfo.do")
	@ResponseBody
	public Map<String, Object> getFeeExecuteInfo(@ModelAttribute FeeExeInfoParamsVO vo, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());
		if (null == vo) {
			resultMap.put("state", "1");
			resultMap.put("msg", "参数不能为空");
			return resultMap;
		}
		String msg = "";
		if (StringUtils.isBlank(regionCode)) {
			msg = "您没有指定统筹区域,请联系管理员,";
		}
		// if (StringUtils.isBlank(vo.getKpiKey())) {
		// msg = "指标不能为空,";
		// }
		if (StringUtils.isBlank(vo.getStartDate())) {
			msg += "开始日期不能为空,";
		} else {
			if (!vo.getStartDate().matches("^[1-9]{1}\\d{3}-(0[1-9]{1}|1[0,1,2]{1})$")) {
				msg += "开始日期不是正确的日期格式,";
			}
		}
		if (StringUtils.isBlank(vo.getEndDate())) {
			msg += "结束日期不能为空,";
		} else {
			if (!vo.getEndDate().matches("^[1-9]{1}\\d{3}-(0[1-9]{1}|1[0,1,2]{1})$")) {
				msg += "结束日期不是正确的日期格式,";
			}
		}
		SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM");
		try {
			if (sft.parse(vo.getStartDate()).getTime() > sft.parse(vo.getEndDate()).getTime()) {
				msg += "开始日期不能大于结束日期,";
			}
		} catch (ParseException e) {
			msg += "开始日期或者结束日期格式不正确,";
		}
		if (StringUtil.isEmpty(msg)) {
			resultMap = feeExecuteInfoService.getFeeInfo(vo.getStartDate(), vo.getEndDate(), vo.getType(), regionCode,
					vo.getKpiKey(), vo.getVisitType());
			resultMap.put("state", "0");
		} else {
			resultMap.put("state", "1");
			resultMap.put("msg", msg);
		}

		return resultMap;
	}

	/**
	 * 费用发生分析
	 */
	@RequestMapping("getFeeHappenAnalysis.do")
	@ResponseBody
	public Map<String, Object> getFeeHappenAnalysis(@ModelAttribute FeeExeInfoParamsVO vo, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String regionCode = this.kpiRegionCodeService.getRegionCode(user.getUserId());
		if (null == vo) {
			resultMap.put("state", "1");
			resultMap.put("msg", "参数不能为空");
			return resultMap;
		}
		String msg = "";
		if (StringUtils.isBlank(regionCode)) {
			msg = "您没有指定统筹区域,请联系管理员,";
		}
		// if (StringUtils.isBlank(vo.getKpiKey())) {
		// msg = "指标不能为空,";
		// }
		if (StringUtils.isBlank(vo.getStartDate())) {
			msg += "开始日期不能为空,";
		} else {
			if (!vo.getStartDate().trim().matches("^[1-9]{1}\\d{3}-(0[1-9]{1}|1[0,1,2]{1})$")) {
				msg += "开始日期不是正确的日期格式,";
			}
		}
		if (StringUtils.isBlank(vo.getEndDate())) {
			msg += "结束日期不能为空,";
		} else {
			if (!vo.getEndDate().trim().matches("^[1-9]{1}\\d{3}-(0[1-9]{1}|1[0,1,2]{1})$")) {
				msg += "结束日期不是正确的日期格式,";
			}
		}
		SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM");
		try {
			if (sft.parse(vo.getStartDate()).getTime() > sft.parse(vo.getEndDate()).getTime()) {
				msg += "开始日期不能大于结束日期,";
			}
		} catch (ParseException e) {
			msg += "开始日期或者结束日期格式不正确,";
		}

		if (StringUtil.isEmpty(msg)) {
			resultMap = feeExecuteInfoService.getFeeAnalysis(vo.getStartDate(), vo.getEndDate(), vo.getType(),
					regionCode, vo.getVisitType());
			resultMap.put("state", "0");
		} else {
			resultMap.put("state", "1");
			resultMap.put("msg", msg);
		}

		return resultMap;
	}
}
